added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / VBASPNETFileUploadStatus / WSFileUploadStatus / Default.aspx.vb
blobb5d7c301bd1ac19555443658ac4ec7d3de17f43b
1 '***************************** Module Header ******************************\
2 '* Module Name: Default.aspx.vb
3 '* Project: VBASPNETFileUploadStatus
4 '* Copyright (c) Microsoft Corporation
5 '*
6 '* The project illustrates how to display the upload status and progress without
7 '* a third part component like ActiveX control, Flash or Silverlight.
8 '*
9 '* This is the page which we test the Upload status for the client
10 '* We use ICallbackEventHandler to realize the communication between
11 '* the server side and client side without refresh the page.
12 '* But we need to use an iframe to hold the upload controls and buttons,
13 '* because the upload need postback to the server, we can't call the server code
14 '* by javascript in one postback page.
15 '* So we let the iframe do the upload postback operation.
16 '*
17 '* For more details about ICallbackEventHandler,
18 '* please read the readme file in the root directory.
19 '*
20 '* This source is subject to the Microsoft Public License.
21 '* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
22 '* All other rights reserved.
23 '\****************************************************************************
27 Imports System.Collections.Generic
28 Imports System.Linq
29 Imports System.Web
30 Imports System.Web.UI
31 Imports System.Web.UI.WebControls
32 Imports System.Threading
33 Imports System.Web.Script.Serialization
34 Imports System.Text
35 Imports VBASPNETFileUploadStatus
38 Partial Public Class _Default
39 Inherits System.Web.UI.Page
40 Implements ICallbackEventHandler
42 Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
43 'Register a client script for ICallbackEventHandler
44 Dim cm As ClientScriptManager = Page.ClientScript
45 Dim cbReference As String = cm.GetCallbackEventReference(Me, "arg", "ReceiveServerData", "")
46 Dim callbackScript As String = "function CallServer(arg, context) {" & cbReference & "; }"
47 cm.RegisterClientScriptBlock(Me.[GetType](), "CallServer", callbackScript, True)
49 End Sub
51 Private uploadModuleProgress As String = ""
52 Public Function GetCallbackResult() As String Implements ICallbackEventHandler.GetCallbackResult
53 Return uploadModuleProgress
54 End Function
56 Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements ICallbackEventHandler.RaiseCallbackEvent
57 If eventArgument = "Clear" Then
58 'operation for clear the cache
59 ClearCache("fuFile")
60 uploadModuleProgress = "Cleared"
61 End If
62 If eventArgument = "Abort" Then
63 'operation for abort uploading
64 AbortUpload("fuFile")
65 uploadModuleProgress = "Aborted"
66 End If
68 Try
69 Dim state As UploadStatus = TryCast(HttpContext.Current.Cache("fuFile"), UploadStatus)
70 If state Is Nothing Then
71 Return
72 End If
73 ' We use JSON to send the data to the client,
74 ' because it is simple and easy to handle.
75 ' For more details about JavaScriptSerializer, please
76 ' read the readme file in the root directory.
77 Dim jss As New JavaScriptSerializer()
79 ' The StringBuilder object will hold the serialized result.
80 Dim sbUploadProgressResult As New StringBuilder()
81 jss.Serialize(state, sbUploadProgressResult)
83 uploadModuleProgress = sbUploadProgressResult.ToString()
84 Catch err As Exception
85 If err.InnerException IsNot Nothing Then
86 uploadModuleProgress = "Error:" + err.InnerException.Message
87 Else
88 uploadModuleProgress = "Error:" + err.Message
89 End If
90 End Try
91 End Sub
93 Private Sub AbortUpload(ByVal cacheID As String)
94 Dim state As UploadStatus = TryCast(HttpContext.Current.Cache(cacheID), UploadStatus)
95 If state Is Nothing Then
96 Return
97 Else
98 state.Abort()
99 End If
100 End Sub
102 Private Sub ClearCache(ByVal cacheID As String)
103 HttpContext.Current.Cache.Remove(cacheID)
104 End Sub
107 End Class